<?php namespace App\Core\Metadata\Data;

class BaseData {
    
    protected $data = [];
    
    public function except($keys){
        $excepts = is_array($keys) ? $keys : func_get_args();
        foreach($excepts as $key){
            if(isset($this->data[$key])){
                unset($this->data[$key]);
            }
        }
        return $this;
    }
    
    public function only($keys){
        $allowedKeys = is_array($keys) ? $keys : func_get_args();
        foreach(array_keys($this->data) as $key){
            if(!in_array($key, $allowedKeys)){
                unset($this->data[$key]);
            }
        }
        return $this;
    }
    
    public function add($data, $overwrite = true){
        if(!is_array($data)){
            throw new \Exception('parameter not validated');
        }
        foreach($data as $key => $value){
            if(isset($this->data[$key]) && !$overwrite){
                continue;
            }
            $this->data[$key] = $value;
        }
        return $this;
    }
    
    public function merge($data){
        $this->add($data, true);
    }
    
    public function filter($callback){
        if(!is_callable($callback)){
            throw new \Exception('parameter not validated');
        }
        $this->data = $callback($this->data);
        return $this;
    }
    
    public function alias($key, $alias, $destroy = true){
        if(!isset($this->data[$key])){
            throw new \Exception('parameter not validated');
        }
        $this->data[$alias] = $this->data[$key];
        if($destroy){
            unset($this->data[$key]);
        }
        return $this;
    }
    
    public function get($key){
        if(!isset($this->data[$key])){
            throw new \Exception('Metadata key "'.$key.'" not found');
        }
        return $this->data[$key];
    }
    
    public function set($key, $value){
        $this->data[$key] = $value;
        return $this;
    }
    
    public function gets(){
        return $this->data;
    }
}